package com.itheima.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.dto.StudentDto;
import com.itheima.domain.pojo.Student;
import com.itheima.domain.vo.PageResult;
import com.itheima.domain.vo.StudentVo;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-08-21 15:48
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<StudentVo> getStudentPage(StudentDto studentDto) {
        PageHelper.startPage(studentDto.getPage(), studentDto.getPageSize());
        Page<StudentVo> P = studentMapper.getStudentPage(studentDto);
        return new PageResult<>(P.getTotal(), P.getResult());
    }

    @Override
    public void deleteStudent(Integer[] ids) {
        studentMapper.deleteStudent(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStudent(Student student) {
        // 判重
        idCardRepeatJudgment(student.getIdCard());
        phoneRepeatJudgment(student.getPhone());
        LocalDateTime now = LocalDateTime.now();
        // 设置时间
        student.setCreateTime(now);
        student.setUpdateTime(now);
        studentMapper.addStudent(student);
    }

    @Override
    public Student getStudentId(Integer id) {
        Student student = studentMapper.getStudentId(id);
        return student;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudent(Student student) {
        LocalDateTime now = LocalDateTime.now();
        student.setUpdateTime(now);
        Student oldStudent = studentMapper.getStudentId(student.getId());
        if (!oldStudent.getNo().equals(student.getNo())){
            // 学号判重
            noRepeatJudgment(student.getNo());
        }
        if (!oldStudent.getIdCard().equals(student.getIdCard())){
            // 身份证号判重
            idCardRepeatJudgment(student.getIdCard());
        }
        if (!oldStudent.getPhone().equals(student.getPhone())){
            // 手机号判重
            phoneRepeatJudgment(student.getPhone());
        }
        studentMapper.updateStudent(student);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pointsWillBeDeductedForDisciplinaryViolations(Integer score, Integer id) {
        if (score < 0 || score > 100) {
            throw new RuntimeException("请输入正常分数");
        }
        Student student = studentMapper.getStudentId(id);
        student.setViolationCount(student.getViolationCount() + 1);
        student.setViolationScore(student.getViolationScore() + score);
    }

    /**
     * 身份证号判重
     */
    private void idCardRepeatJudgment(String idCard) {
        Integer count = studentMapper.getStudentIdCardCount(idCard);
        if (count > 0) {
            throw new RuntimeException("身份证号已存在");
        }
    }

    /**
     * 手机号判重
     */
    private void phoneRepeatJudgment(String phone) {
        Integer count = studentMapper.getStudentPhoneCount(phone);
        if (count > 0) {
            throw new RuntimeException("手机号已存在");
        }
    }

    /**
     * 学号判重
     */
    private void noRepeatJudgment(String no){
        Integer count = studentMapper.getStudentNoCount(no);
        if (count > 0) {
            throw new RuntimeException("学号已存在");
        }
    }


}
